package com.codefish.codefish_blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codefish.codefish_blog.entity.ArchiveInfo;
import com.codefish.codefish_blog.entity.ArticleSnapshot;
import com.codefish.codefish_blog.entity.Category;
import com.codefish.codefish_blog.entity.ClassifyInfo;
import com.codefish.codefish_blog.service.ArticleService;
import com.codefish.codefish_blog.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * web页面controller类
 *
 * @author codefish
 * @version 1.0
 * @date 2022/03/22 下午 09:37
 */
@Controller
public class WebController {
    @Autowired
    ArticleService articleService;
    @Autowired
    CategoryService categoryService;

    /**
     * 显示index页面
     *
     * @param model   model对象
     * @param session session对象
     * @return index页面
     */
    @GetMapping({"/", "/index"})
    public String indexPage(Model model, @RequestParam(value = "page", defaultValue = "1") int page, HttpSession session) {
        //获取文章总篇数
        int totalSize = articleService.list().size();
        // 按照5条一页返回文章快照(第一页返回8条，前3条做为热点文章)
        List<ArticleSnapshot> articleSnapshots = articleService.listArticleSnapshots(new Page<>(page, (page == 1) ? 8 : 5));
        model.addAttribute("nowPage", page);
        model.addAttribute("totalPage", (int) Math.ceil(totalSize / 5.0));
        model.addAttribute("articleSnapshots", articleSnapshots);
        //查找最新文章列表
        List<ArticleSnapshot> newArticleSnapshots = articleService.listNewArticleSnapshots();
        session.setAttribute("newArticle", newArticleSnapshots);
        return "blog-index";
    }

    /**
     * 显示文章归档页面
     *
     * @param model   model对象
     * @param session session对象
     * @return archive页面
     */
    @GetMapping("/archive")
    public String archivePage(Model model, HttpSession session) {
        //获取文章总篇数
        int totalSize = articleService.list().size();
        model.addAttribute("totalSize", totalSize);
        //获取文章归档列表
        List<List<ArchiveInfo>> archiveList = articleService.listSimplyOrderByDate();
        model.addAttribute("archiveList", archiveList);
        //查找最新文章列表
        List<ArticleSnapshot> newArticleSnapshots = articleService.listNewArticleSnapshots();
        session.setAttribute("newArticle", newArticleSnapshots);
        return "blog-archive";
    }

    @GetMapping("/classify")
    public String classifyPage(@RequestParam(defaultValue = "") String tag, Model model, HttpSession session) {
        //查找所有分类
        List<Category> categories = categoryService.list();
        model.addAttribute("categories", categories);
        //按传入的分类查找文章
        if ("".equals(tag)) {
            tag = categories.get(0).getName();
        }
        List<ClassifyInfo> classifyList = articleService.listArticleSnapshotsByCategory(tag);
        model.addAttribute("classifyList",classifyList);
        model.addAttribute("articleSize", classifyList.size());
        model.addAttribute("nowTag", tag);
        //查找最新文章列表
        List<ArticleSnapshot> newArticleSnapshots = articleService.listNewArticleSnapshots();
        session.setAttribute("newArticle", newArticleSnapshots);
        return "blog-classify";
    }
}
